<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<form action="success.html" onsubmit="return check()"><!--返回值阻止事件默认行为-->
			姓名: <input type="text" id="username"><span id="name"></span>
			<br>
			密码: <input type="password" id="userPassword"><span id="pass"></span>
			<br>
			年龄: <input type="text" id="inputAge"><span id="age"></span>
			<br>
			手机: <input type="text" id="inputPhone"><span id="phone"></span>
			<br>
			<input type="submit" value="提交"/>
		</form>
		
		<script type="text/javascript">
			function check(){
				let name= document.getElementById("username").value;
				let password=document.getElementById("userPassword").value;
				let age=document.getElementById("inputAge").value;
				let phone=document.getElementById("inputPhone").value;
				let flag=true;
				if(name.trim()==""){
					document.getElementById("name").innerHTML="姓名不能为空";
					flag=false;
				}else{
					document.getElementById("name").innerHTML="姓名验证通过";
				}
				if(password.length<6){
					document.getElementById("pass").innerHTML="密码不少于6位";
					flag=false;
				}else{
					document.getElementById("pass").innerHTML="密码验证通过";
				}
				if(isNaN(age)||age<0||age>150){
					document.getElementById("age").innerHTML="年龄必须位数字且年龄在0~150之间";
					flag=false;
				}else{
					document.getElementById("age").innerHTML="年龄验证通过";
				}
				let reg=/^[1][\d]{10}$/;
				if(!reg.test(phone)){
					document.getElementById("phone").innerHTML="手机号格式错误";
					flag=false;
				}else{
					document.getElementById("phone").innerHTML="手机号验证通过";
				}
				return flag;
			}
			
		</script>
	</body>
</html>
